import App from './App'

//接口url
Vue.prototype.websiteUrl = 'http://www.fittingphone.com'; //http://learn.yileui.com http://sm.pattonlex.com http://sm.76741.com http://www.fittingphone.com
//图片url 
Vue.prototype.webImageUrl = 'http://www.fittingphone.com'; //http://47.97.66.109:9068
//sid和key
Vue.prototype.wc_sid = 'bab0c2fe-7e28-11ed-97d3-525400150d26';
Vue.prototype.wc_key = 'bf30efc1-7e28-11ed-97d3-525400150d26';

//方法 - 弹窗
Vue.prototype.fun_showToast = function(title) {
	uni.showToast({
		title: title,
		icon: 'none'
	});
}
//方法 - 刷新
Vue.prototype.fun_loading = function(title) {
	uni.showLoading({
		title: title
	});
}
//方法 - 咨询框
Vue.prototype.fun_showModal = function(content, confirm, title, cancel) {
	uni.showModal({
		title: title ? title : '提示',
		content: content,
		success: function(res) {
			if (res.confirm) {
				if (confirm) confirm();
			} else if (res.cancel) {
				if (cancel) cancel();
			}
		}
	});
}
//方法 - 跳转
Vue.prototype.fun_navigateTo = function(url) {
	uni.navigateTo({
		url: url
	});
}
//方法 - 切换到 tabbar 页面
Vue.prototype.fun_switchTab = function(url) {
	uni.switchTab({
		url: url
	});
}

//方法 - 接口调用
Vue.prototype.fun_request = function(url, data, fun, fail, isLoad, method) {
	if (isLoad) uni.showLoading();
	// //判断是否需要登录
	// if (isLogin) {
	// 	var user_code = uni.getStorageSync('user_code');
	// 	if (user_code) url = url + "?user_code=" + user_code;
	// }
	if (data) {
		data.wc_sid = this.wc_sid;
		data.wc_key = this.wc_key;
	}

	uni.request({
		url: this.websiteUrl + url, //仅为示例，并非真实接口地址。
		data: data,
		method: method ? method : "POST",
		dataType: "json",
		header: {
			'content-type': (method == "POST" || method == undefined) ? 'application/json' :
				'application/x-www-form-urlencoded',
			'Authorization': 'Bearer ' + uni.getStorageSync('token')
		},
		success: (res) => {
			res.data.code == 1 ? (fail != null ? fail(res.data) : "") : fun(res.data);
		},
		fail: (res) => {
			if (fail != null) fail(res.data);
		},
		complete: function(res) {
			if (isLoad) uni.hideLoading();
			
			if (res.statusCode == '401') {
				uni.navigateTo({
					url: '/pages/login/index'
				});
				
				uni.hideLoading();
				res.status = "login";
				fun(res);
			}
		}
	});
}
//方法 - 验证
Vue.prototype.fun_validate = function(error, msg) {
	if (error) {
		this.fun_showToast(msg);
	}
	return error;
}
//方法 - 循环
Vue.prototype.fun_map = function(list, fn) {
	for (var i = 0, len = list.length; i < len; i++) fn(i, list[i]);
}

import uView from '@/uni_modules/uview-ui'
Vue.use(uView)

import VueI18n from "vue-i18n";
Vue.use(VueI18n);

const i18n = new VueI18n({
    locale: uni.getStorageSync('lang') == (undefined || "") ? "pt_BR" : uni.getStorageSync('lang'),
    messages: {
        zh_CN: require("./static/lang/zh_CN.json"),
        pt_BR: require("./static/lang/pt_BR.json")
    }
});
Vue.prototype._i18n = i18n;

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	i18n,
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif
